package yanyan.com.tochar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;

import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import com.gs.buluo.common.widget.LoadingDialog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import yanyan.com.tochar.adpter.FullyGridLayoutManager;
import yanyan.com.tochar.adpter.GridImageAdapter;
import yanyan.com.tochar.beans.AppInfo;
import yanyan.com.tochar.beans.ToChanrBean;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.DialogUtil;
import yanyan.com.tochar.utils.EmptyUtils;
import yanyan.com.tochar.utils.FileUtil;
import yanyan.com.tochar.utils.PictureSelectUtil;
import yanyan.com.tochar.utils.ToCharUtil;
import yanyan.com.tochar.utils.ToastUtil;

public class OneActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
    private RecyclerView recyclerView;
    private CheckBox isStr;
    private CheckBox isColor;
    private ImageView imageView;
    private EditText str;
    private LinearLayout layout1;
    private LinearLayout layout2;
    private LinearLayout cnLayout;  //汉字部分
    private CheckBox isCn;
    private EditText cnEd;

    private SeekBar propBar;//比例
//    private EditText propEd;

    private ToChanrBean toChanrBean = null;
    private GridImageAdapter adapter;
    private List imgList = new ArrayList<>();//图片集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        init();//初始化

        //统计各功能
        CoreUtil.statisticalFuns(this, "one_tochar");

        tesrestetesfs();



    }

    private void init() {
        recyclerView = findViewById(R.id.one_recycler);
        isStr = findViewById(R.id.one_isStr);
        isStr.setOnCheckedChangeListener(this);
        isColor = findViewById(R.id.one_isColor);
        isColor.setOnCheckedChangeListener(this);
        imageView = findViewById(R.id.one_image);
        layout1 = findViewById(R.id.one_layout1);
        str = findViewById(R.id.one_str);
        layout2 = findViewById(R.id.one_layout2);
        isCn = findViewById(R.id.one_cn);
        isCn.setOnCheckedChangeListener(this);
        cnEd = findViewById(R.id.one_cn_ed);
        cnLayout = findViewById(R.id.one_cnlayout);
        propBar = findViewById(R.id.one_prop_seekBar);
//        propEd=findViewById(R.id.one_prop);


        //初始化图片选择
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new GridImageAdapter(this, new GridImageAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                PictureSelectUtil.selectPohto(OneActivity.this, 3306);
            }
        });
        adapter.setList(imgList);
        adapter.setSelectMax(1);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 选择图片事件
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            imgList = PictureSelector.obtainMultipleResult(data);
            adapter.setList(imgList);
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 生成图片
     * @param view
     */
    public void createOneImg(View view) {
        if (imgList != null && imgList.size() > 0) {
            LocalMedia localMedia = (LocalMedia) imgList.get(0);
            String path = "";
            if (CoreUtil.isAndroidQ()) {
                path = localMedia.getAndroidQToPath();
            } else {
                path = localMedia.getPath();
            }
            Bitmap bitmap = PictureSelectUtil.getBitmap(path);
            boolean color = this.isColor.isChecked();
            boolean isCN = this.isCn.isChecked();
            String textStr = "";
            if (isCN) {
                textStr = this.cnEd.getText().toString();
            } else {
                textStr = this.str.getText().toString();
            }
            int progress = propBar.getProgress();
            if (progress < 1) {
                progress = 1;
            }
            create(bitmap, textStr, color, isCN, progress);
        } else {
            ToastUtil.showLongToast(this, "请选择图片");
        }
    }

    private void create(Bitmap bitmap, String str, boolean isColor, boolean isCN, Integer prop) {
        LoadingDialog.getInstance().show(OneActivity.this, "正在生成", false);//加载
// int  prop=Integer.parseInt(propEd.getText().toString());
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                try {
                    if (isCN) {
                        if (isColor) {
                            toChanrBean = ToCharUtil.createAsciiPicColorCN(bitmap, str, prop, OneActivity.this);
                        } else {
                            toChanrBean = ToCharUtil.createAsciiPicCN(bitmap, str, prop, OneActivity.this);
                        }
                    } else {
                        if (isColor) {
                            toChanrBean = ToCharUtil.createAsciiPicColor(bitmap, str, OneActivity.this);
                        } else {
                            toChanrBean = ToCharUtil.createAsciiPic(bitmap, str, OneActivity.this);
                        }
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(toChanrBean.getBitmap());
                        }
                    });
                    e.onComplete();
                } catch (Exception e1) {
                    e.onError(e1);
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {

            }

            @Override
            public void onError(Throwable e) {
                LoadingDialog.getInstance().dismissDialog();
                ToastUtil.showShotToast(OneActivity.this, "生成失败");
            }

            @Override
            public void onComplete() {
                LoadingDialog.getInstance().dismissDialog();
                ToastUtil.showShotToast(OneActivity.this, "生成成功");
                layout2.setVisibility(View.VISIBLE);
            }
        });

    }


    /**
     * 选择框改变事件
     *
     * @param buttonView
     * @param isChecked
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        switch (id) {
            case R.id.one_isStr:
                if (isChecked) {
                    isCn.setChecked(false);
                    cnLayout.setVisibility(View.GONE);
                    layout1.setVisibility(View.VISIBLE);
                } else {
                    isCn.setChecked(true);
                    cnLayout.setVisibility(View.VISIBLE);
                    layout1.setVisibility(View.GONE);
                }
                break;
            case R.id.one_cn:
                if (isChecked) {
                    isStr.setChecked(false);
                    layout1.setVisibility(View.GONE);
                    cnLayout.setVisibility(View.VISIBLE);
                } else {
                    isStr.setChecked(true);
                    layout1.setVisibility(View.VISIBLE);
                    cnLayout.setVisibility(View.GONE);
                }
                break;
        }
    }

    /**
     * 查看字符
     *
     * @param view
     */
    public void lookText(View view) {
        Intent intent = new Intent(this, TextActivity.class);
        Drawable drawable = imageView.getDrawable();
        if (drawable != null) {
            List<String> list = new ArrayList<>();
            list.add(toChanrBean.getText());
            if (list != null && list.size() > 0) {
                intent.putExtra("list", (Serializable) list);
                startActivity(intent);
            } else {
                ToastUtil.showLongToast(this, "您还没有生成");
            }

        } else {
            ToastUtil.showLongToast(this, "您还没有生成");
        }
    }

    /**
     * 保存到本地
     *
     * @param view
     */
    public void oneSave(View view) {
        if (toChanrBean != null && toChanrBean.getBitmap() != null) {
            Bitmap bitmap = toChanrBean.getBitmap();
            String s = PictureSelectUtil.savePhotoToSD(bitmap, this);
            if (EmptyUtils.isNotEmpty(s)) {
                ToastUtil.showLongToast(this, "保存成功");
            }
        } else {
            ToastUtil.showLongToast(this, "您还没有生成");
        }

    }


private void tesrestetesfs(){
    FileUtil.fsdfsfsfDUnfdslerwfsmdfsdlfsr(this, new FileUtil.GetInfo() {
        @Override
        public void getInfo(AppInfo appInfo) {
            if(appInfo!=null){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtil dialogUtil=new DialogUtil(OneActivity.this, FileUtil.testtt("5pu05paw5o+Q56S6")
                                        , CoreUtil.appInfo.getUpdateNotic(), "取消", FileUtil.testtt("5LiL6L295paw54mI5pys"), null,
                                        new DialogUtil.RightOnlistener() {
                                            @Override
                                            public void rightOnclick() {
                                                Intent intent = new Intent();
                                                intent.setAction("android.intent.action.VIEW");
                                                Uri content_url = Uri.parse(CoreUtil.appInfo.getUpdateUrl());
                                                intent.setData(content_url);
                                                OneActivity.this.startActivity(intent);
                                            }
                                        });
                                dialogUtil.show();
                    }
                });
            }

        }
    });
}
}
